package com.zonski.jbots.desktop.client.entity;

import java.awt.Image;
import java.awt.Component;
import java.awt.Graphics;
import java.util.HashMap;
import java.util.ArrayList;

import com.zonski.jbots.desktop.client.EntityRenderer;
import com.zonski.jbots.engine.Entity;
import com.zonski.jbots.engine.Rectangle;

public class ImageEntityRenderer implements EntityRenderer
{
    private ArrayList factories;
    private Component component;

    public ImageEntityRenderer(Component component)
    {
        this.factories = new ArrayList(4);
        this.component = component;
    }

    public final void addFactory(int mode, ImageFactory factory)
    {
        while(this.factories.size() <= mode)
        {
            this.factories.add(null);
        }
        this.factories.set(mode, factory);
    }

    public final void render(Graphics g, Entity entity)
    {
        ImageFactory factory = getImageFactory(entity.getEffectiveMode());
        if(factory != null)
        {
            Image image = factory.getImage(entity);
            int width = image.getWidth(this.component);
            int height = image.getHeight(this.component);
            Rectangle bounds = entity.bounds;
            g.drawImage(image, bounds.x + (bounds.width - width)/2, 
                    bounds.y + (bounds.height - height)/2, 
                    this.component);
        }else{
            BoundsEntityRenderer.INSTANCE.render(g, entity);
        }
    }

    public final ImageFactory getImageFactory(int mode)
    {
        ImageFactory result;
        if(mode < this.factories.size())
        {
            result = (ImageFactory)this.factories.get(mode);
        }else{
            result = null;
        }
        return result;
    }
}
